package pt.isel.pdm.ouryamba;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Represents the class that will bind to my database and will retrieve appropriate objects to write
 * and read from the database.
 * */
public class DbHelper extends SQLiteOpenHelper
{
	/**
	 * Represents the name of the database.
	 * */
	public static final String DB_NAME = "Our_Yamba_DB";
	/**
	 * Represents the unique version of the database.
	 * */
	public static final int DB_VERSION = 1;
	
	/**
	 * Creates a database if no exists yet with the name and version defined.
	 * 
	 * @see #DB_NAME
	 * @see #DB_VERSION
	 * */
	public DbHelper(Context context) 
	{
		super(context, DB_NAME, null, DB_VERSION);
	}
	
	/**
	 * Creates the table Status if it does not exist yet.
	 * 
	 * @see DbStatusContract
	 * @see DbStatusContract#ID
	 * @see DbStatusContract#CREATED_AT
	 * @see DbStatusContract#USER_ID
	 * @see DbStatusContract#USER_NAME
	 * @see DbStatusContract#TABLE
	 * */
	@Override
	public void onCreate(SQLiteDatabase db)
	{
		String query = DbStatusContract.ID + " integer not null,"
		   + DbStatusContract.CREATED_AT + " date not null,"
		   + DbStatusContract.MESSAGE + " text not null,"
		   + DbStatusContract.USER_NAME + " text not null,"
		   + DbStatusContract.COMMITTED + " boolean not null";
		query = "CREATE TABLE " + DbStatusContract.TABLE + "(" + query + ")";
		db.execSQL(query);
	}

	/**
	 * Upgrades the database by dropping all tables and build all again with the call to {@code onCreate()} method.
	 * 
	 * 
	 * @see DbStatusContract
	 * @see DbStatusContract#TABLE 
	 * @see #onCreate(SQLiteDatabase)
	 * */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
	{
		db.execSQL("DROP TABLE if exists " + DbStatusContract.TABLE);
		onCreate(db);
	}

}
